package com.qikecn.utils;

import com.qikecn.api.model.Config;
import com.qikecn.api.model.User;
import org.json.JSONObject;

import cn.geekapp.utils.StringUtils;

import com.qikecn.api.common.Contents;
import com.qikecn.baidu.yun.push.utils.BaiduPushIosUtilNormal;
import com.qikecn.baidu.yun.push.utils.BaiduPushUtilNormal;

public class PushUtil {
	
	/**
	 * 消息通知接口-普通用户
	 * @param userid
	 * @param title
	 * @param desc
	 * @param params
	 */
	public static final boolean notifyForUser(String userid, String title, String desc, String params){
		User user = User.dao.findFirst("select zongji,pushuserid,pushchannelid,clienttype from "+Contents.TAB_PREFIX+"user where id='"+userid+"' limit 1");
		if(user == null || StringUtils.isEmpty(user.getPushchannelid()) || StringUtils.isEmpty(user.getClienttype())){
			return false;
		}

		String baidu_push_api_key = "";
		String baidu_push_secret_key = "";

		String clienttype = user.getClienttype();
		if( clienttype.equalsIgnoreCase("ios") || clienttype.equalsIgnoreCase("iphone") ){
			Config config = Config.dao.findFirstByCache(Contents.MYCACHE, "notifyForUser_baidu_push_api_key_ios", "select config_value from "+Contents.TAB_PREFIX+"config where zongji='"+user.getZongji()+"' and config_key='baidu_push_api_key_ios' limit 1");
			baidu_push_api_key = config != null ? config.getConfigValue() : "";
			config = Config.dao.findFirstByCache(Contents.MYCACHE, "notifyForUser_baidu_push_secret_key_ios", "select config_value from "+Contents.TAB_PREFIX+"config where zongji='"+user.getZongji()+"' and config_key='baidu_push_secret_key_ios' limit 1");
			baidu_push_secret_key = config != null ? config.getConfigValue() : "";
		}else {
			Config config = Config.dao.findFirstByCache(Contents.MYCACHE, "notifyForUser_baidu_push_api_key", "select config_value from "+Contents.TAB_PREFIX+"config where zongji='"+user.getZongji()+"' and config_key='baidu_push_api_key' limit 1");
			baidu_push_api_key = config != null ? config.getConfigValue() : "";
			config = Config.dao.findFirstByCache(Contents.MYCACHE, "notifyForUser_baidu_push_secret_key", "select config_value from "+Contents.TAB_PREFIX+"config where zongji='"+user.getZongji()+"' and config_key='baidu_push_secret_key' limit 1");
			baidu_push_secret_key = config != null ? config.getConfigValue() : "";
		}
		return notify(baidu_push_api_key, baidu_push_secret_key, user.getPushuserid(), user.getPushchannelid(), user.getClienttype(), title, desc, params);
	}

	/**
	 * 推送通知
	 * @param api_key
	 * @param secret_key
	 * @param pushuserid
	 * @param pushchannelid
	 * @param clienttype
	 * @param title
	 * @param desc
	 * @param params
	 * @return
	 */
	public static final boolean notify(String api_key, String secret_key, String pushuserid, String pushchannelid, String clienttype, 
			String title, String desc, String params){
		if(StringUtils.isEmpty(clienttype) || StringUtils.isEmpty(pushchannelid) || StringUtils.isEmpty(api_key) || StringUtils.isEmpty(secret_key)){
			return false;
		}
		try {
			if( clienttype.equalsIgnoreCase("android") ){//android
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("title", title);
				jsonObject.put("content", desc);
				jsonObject.put("type", 0);
				BaiduPushUtilNormal utilNormal = null;
				utilNormal = new BaiduPushUtilNormal(api_key, secret_key);
				utilNormal.pushMsgToSingleDevice(pushchannelid, pushuserid, jsonObject.toString());
			}else if( clienttype.equalsIgnoreCase("ios") || clienttype.equalsIgnoreCase("iphone") ){
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("type", 0);
				JSONObject aps = new JSONObject();
				jsonObject.put("aps", aps);
				
				JSONObject alertObj = new JSONObject();
				alertObj.put("title", title);
				alertObj.put("body", "["+title+"]"+desc);
				
				aps.put("alert", alertObj);
				aps.put("sound", "default");
				aps.put("badge", 0);
				BaiduPushIosUtilNormal utilNormal = new BaiduPushIosUtilNormal(api_key, secret_key, 2);
				utilNormal.pushMsgToSingleDevice(pushchannelid, pushuserid, jsonObject.toString());
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return true;
	}
	
	public static void main(String[] args) {
		
		notify("ga2nXPGNUI6yLmlUtVD2tE0p", "53NdylSwS6hu3xh7rUL61q2VAwdoByVe", "638223647019701697", "4751897202878968981", "ios", "测试推送标题", "测试推送内容", "");
		
	}
	
}
